package cn.xshi.oauth.service.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.xshi.oauth.dao.OauthKeyInfoDao;
import cn.xshi.oauth.model.OauthKeyInfo;
import cn.xshi.oauth.service.OauthKeyInfoService;
import cn.xshi.common.cache.redis.RedisUtil;
import cn.xshi.common.constant.CacheConstant;
import cn.xshi.common.idgeneration.UUID;
import cn.xshi.common.util.JsonUtil;
import cn.xshi.common.base.BaseService;
import cn.xshi.common.util.ExceptionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
/**
 * @Desc 授权中心密钥管理
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
@Slf4j
public class OauthKeyInfoServiceImpl extends BaseService implements OauthKeyInfoService {

	@Resource
	private OauthKeyInfoDao oauthKeyInfoDao;

	@Resource
	private RedisUtil redisUtil;

	/**
	* 分页
	* @param condition 
	* @return
	*/
	public List<OauthKeyInfo> getOauthKeyInfoListByCondition(Map<String,Object> condition){
		try{
			return oauthKeyInfoDao.getOauthKeyInfoListByCondition(condition);
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 查询对象
	* @param id
	* @return
	*/
	public OauthKeyInfo getOauthKeyInfoById(String id){
		try{
			OauthKeyInfo oauthKeyInfo = oauthKeyInfoDao.getOauthKeyInfoById(id);
			return oauthKeyInfo;
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
	}

	/**
	* 添加
	* @param oauthKeyInfo 
	* @return
	*/
	public int addOauthKeyInfo(OauthKeyInfo oauthKeyInfo){
		int i = 0;
		try {
			oauthKeyInfo.setId(UUID.toUUID());
			oauthKeyInfo.setCreateId(getCurrentAccountId());
			oauthKeyInfo.setCreateTime(getDate());
			i = oauthKeyInfoDao.addOauthKeyInfo(oauthKeyInfo);
			sync();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改
	* @param oauthKeyInfo 
	* @return
	*/
	public int updateOauthKeyInfo(OauthKeyInfo oauthKeyInfo){
		int i = 0;
		try {
			oauthKeyInfo.setUpdateId(getCurrentAccountId());
			oauthKeyInfo.setUpdateTime(getDate());
			i = oauthKeyInfoDao.updateOauthKeyInfo(oauthKeyInfo);
			sync();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 修改（根据动态条件）
	* @param oauthKeyInfo 
	* @return
	*/
	public int updateOauthKeyInfoBySelective(OauthKeyInfo oauthKeyInfo){
		int i = 0;
		try {
			oauthKeyInfo.setUpdateId(getCurrentAccountId());
			oauthKeyInfo.setUpdateTime(getDate());
			i = oauthKeyInfoDao.updateOauthKeyInfoBySelective(oauthKeyInfo);
			sync();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 删除
	* @param condition 
	* @return
	*/
	public int delOauthKeyInfo(Map<String,Object> condition){
		int i = 0;
		try {
			i = oauthKeyInfoDao.delOauthKeyInfo(condition);
			sync();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量修改
	* @param oauthKeyInfoList 
	* @return
	*/
	public int updateBatchOauthKeyInfo(List<OauthKeyInfo> oauthKeyInfoList){
		int i = 0;
		try {
			i = oauthKeyInfoDao.updateBatchOauthKeyInfo(oauthKeyInfoList);
			sync();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	* 批量修改（根据动态条件）
	* @param oauthKeyInfoList 
	* @return
	*/
	public int updateBatchOauthKeyInfoBySelective(List<OauthKeyInfo> oauthKeyInfoList){
		int i = 0;
		try {
			i = oauthKeyInfoDao.updateBatchOauthKeyInfoBySelective(oauthKeyInfoList);
			sync();
		} catch (Exception e) {
			throw new ExceptionUtil(e.getMessage(),e.getCause());
		}
		return i;
	}

	/**
	 * 同步缓存
	 * @return
	 */
	public boolean sync(){
		try {
			redisUtil.del(CacheConstant.OAUTH_KEY);
			List<OauthKeyInfo> list = getOauthKeyInfoListByCondition(new HashMap<>());
			for(OauthKeyInfo oauthKeyInfo:list){
				redisUtil.hset(CacheConstant.OAUTH_KEY,oauthKeyInfo.getAppKey(), JsonUtil.toFastJson(oauthKeyInfo));
			}
			return true;
		}catch (Exception e){
			log.info("同步密钥缓存失败，错误信息：{}",e);
			return false;
		}
	}
}
